package com.genersoft.iot.vmp.storager.dao;

import com.genersoft.iot.vmp.gb28181.bean.Device;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 用于存储设备信息
 */
@Mapper
@Repository
public interface DeviceMapper {

    @Select("SELECT * FROM device WHERE device_id = #{deviceId}")
    @Results(id="device",
    value = { 
       @Result(property = "deviceId", column = "device_id"),
       @Result(property = "name", column = "name"), 
       @Result(property = "manufacturer", column = "manufacturer"),
       @Result(property = "model", column = "model"),
       @Result(property = "firmware", column = "firmware"), 
       @Result(property = "transport", column = "transport"), 
       @Result(property = "streamMode", column = "stream_mode"), 
       @Result(property = "ip", column = "ip"), 
       @Result(property = "port", column = "port"), 
       @Result(property = "hostAddress", column = "host_address"), 
       @Result(property = "expires", column = "expires"), 
       @Result(property = "registerTime", column = "register_time"), 
       @Result(property = "keepaliveTime", column = "keepalive_time"), 
       @Result(property = "createTime", column = "create_time"), 
       @Result(property = "updateTime", column = "update_time"), 
       @Result(property = "online", column = "onlines"), 
       })
    Device getDeviceByDeviceId(String deviceId);

    @Insert("INSERT INTO device (" +
                "device_id, " +
                "name, " +
                "manufacturer, " +
                "model, " +
                "firmware, " +
                "transport," +
                "stream_mode," +
                "ip," +
                "port," +
                "host_address," +
                "expires," +
                "register_time," +
                "keepalive_time," +
                "create_time," +
                "update_time," +
                "onlines" +
            ") VALUES (" +
                "#{deviceId}," +
                "#{name,jdbcType=VARCHAR}," +
                "#{manufacturer,jdbcType=VARCHAR}," +
                "#{model,jdbcType=VARCHAR}," +
                "#{firmware,jdbcType=VARCHAR}," +
                "#{transport,jdbcType=VARCHAR}," +
                "#{streamMode,jdbcType=VARCHAR}," +
                "#{ip,jdbcType=VARCHAR}," +
                "#{port}," +
                "#{hostAddress,jdbcType=VARCHAR}," +
                "#{expires}," +
                "#{registerTime}," +
                "#{keepaliveTime}," +
                "#{createTime}," +
                "#{updateTime}," +
                "#{online}" +
            ")")
    int add(Device device);

    @Update(value = {" <script>" +
                "UPDATE device " +
                "SET update_time='${updateTime}'" +
                "<if test=\"name != null\">, name='${name}'</if>" +
                "<if test=\"manufacturer != null\">, manufacturer='${manufacturer}'</if>" +
                "<if test=\"model != null\">, model='${model}'</if>" +
                "<if test=\"firmware != null\">, firmware='${firmware}'</if>" +
                "<if test=\"transport != null\">, transport='${transport}'</if>" +
                "<if test=\"streamMode != null\">, stream_mode='${streamMode}'</if>" +
                "<if test=\"ip != null\">, ip='${ip}'</if>" +
                "<if test=\"port != null\">, port=${port}</if>" +
                "<if test=\"hostAddress != null\">, host_address='${hostAddress}'</if>" +
                "<if test=\"online != null\">, onlines=${online}</if>" +
                "<if test=\"registerTime != null\">, register_time='${registerTime}'</if>" +
                "<if test=\"keepaliveTime != null\">, keepalive_time='${keepaliveTime}'</if>" +
                "<if test=\"expires != null\">, expires=${expires}</if>" +
                "WHERE device_id='${deviceId}'"+
            " </script>"})
    int update(Device device);

    @Select("SELECT de.*, (SELECT count(0) FROM device_channel WHERE device_id=de.device_id) as channelCount  FROM device de")
    @ResultMap("device")
    @Result(property = "channelCount", column = "channelCount")
    List<Device> getDevices();

    @Delete("DELETE FROM device WHERE device_id=#{deviceId}")
    int del(String deviceId);

    @Update("UPDATE device SET onlines=0")
    int outlineForAll();
}
